package com.pinyougou.sellergoods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pinyougou.dao.TypeTemplateDao;
import com.pinyougou.entity.PageResult;
import com.pinyougou.pojo.TypeTemplate;
import com.pinyougou.sellergoods.service.TypeTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional
public class TypeTemplateServiceImpl implements TypeTemplateService {
    @Autowired
    private TypeTemplateDao typeTemplateDao;

    @Override
    public List<TypeTemplate> findAll() {
        List<TypeTemplate> typeTemplateList = typeTemplateDao.selectAll();
        return typeTemplateList;
    }

    @Override
    public PageResult<TypeTemplate> findByPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<TypeTemplate> typeTemplateList = typeTemplateDao.selectAll();
        PageInfo<TypeTemplate> pageInfo = new PageInfo<>(typeTemplateList);
        PageResult<TypeTemplate> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }

    @Override
    public void save(TypeTemplate typeTemplate) {
        typeTemplateDao.insert(typeTemplate);
    }

    @Override
    public TypeTemplate findById(Integer id) {
        TypeTemplate typeTemplate = typeTemplateDao.selectByPrimaryKey(id);
        return typeTemplate;
    }

    @Override
    public void update(TypeTemplate typeTemplate) {
        typeTemplateDao.updateByPrimaryKey(typeTemplate);
    }

    @Override
    public void delete(Integer[] ids) {
        Example example=new Example(TypeTemplate.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", Arrays.asList(ids));
        typeTemplateDao.deleteByExample(example);
    }

    @Override
    public PageResult<TypeTemplate> findByPage(TypeTemplate typeTemplate, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Example example = null;
        if (typeTemplate != null) {
            example = new Example(TypeTemplate.class);
            Example.Criteria criteria = example.createCriteria();
            if (StringUtils.isNotBlank(typeTemplate.getName())) {
                criteria.andLike("name", "%" + typeTemplate.getName() + "%");
            }
        }
        List<TypeTemplate> typeTemplateList = typeTemplateDao.selectByExample(example);
        PageInfo<TypeTemplate> pageInfo = new PageInfo<>(typeTemplateList);
        PageResult<TypeTemplate> pageResult = new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
        return pageResult;
    }
}
